Skip to content

fix(accounts): add migration to repair missing selectedAccount#27717

Closed
tommasini wants to merge 1 commit into
mainfrom
chore/selected-account-undefined
Closed

fix(accounts): add migration to repair missing selectedAccount#27717
tommasini wants to merge 1 commit into
mainfrom
chore/selected-account-undefined

Conversation

@tommasini
Copy link
Copy Markdown
Contributor

Description

Sentry reports show users crashing on v7.66.0 with Account Id "undefined" not found during Engine initialization. The root cause is a corrupted AccountsController.internalAccounts.selectedAccount that is either missing or undefined in persisted state.

Migration 059 attempted to fix this but has a critical gap: it uses hasProperty(internalAccounts, 'selectedAccount') which returns false when the key is entirely absent (the common case after a JSON.stringify/JSON.parse roundtrip strips undefined values). This means migration 059 silently skips the most common form of this corruption.

Migration 126 fixes this by:

  • Detecting both a missing key and an explicit undefined value
  • Reporting the exact corruption type (key_missing, value_undefined, wrong_type_*, empty_string) to Sentry for diagnostics
  • Recovering by setting selectedAccount to the first account ID, or falling back to '' (which AccountsController auto-reconciles)

Since this is migration 126 (> 107), it runs after inflateFromControllers pulls the per-controller filesystem data into engine.backgroundState, so it operates on the actual corrupted data regardless of its origin.

Changelog

CHANGELOG entry: Fixed a crash caused by corrupted account selection state during app upgrade

Related issues

Fixes: #11488

Manual testing steps

Feature: Migration 126 repairs missing selectedAccount

  Scenario: user upgrades with corrupted AccountsController state
    Given the user has a persisted AccountsController state where selectedAccount key is missing from internalAccounts
    When the user upgrades to the new app version
    Then migration 126 detects the missing key and sets selectedAccount to the first account ID
    And the app starts successfully without crashing

  Scenario: user upgrades with valid AccountsController state
    Given the user has a persisted AccountsController state where selectedAccount is a valid account ID
    When the user upgrades to the new app version
    Then migration 126 is a no-op and the state is unchanged

Screenshots/Recordings

Before

N/A

After

N/A

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Made with Cursor

@tommasini tommasini self-assigned this Mar 19, 2026
@github-actions
Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot metamaskbot added the team-mobile-platform Mobile Platform team label Mar 19, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

⏭️ Smart E2E selection skipped - draft PR

All E2E tests pre-selected.

View GitHub Actions results

@github-project-automation github-project-automation Bot moved this to Needs dev review in PR review queue Mar 19, 2026
? `wrong_type_${typeof currentValue}`
: 'empty_string';

captureException(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we want to remove this exception being captured, it seems that will not help with really anything if we get to this point, only pollute sentry

@github-actions
Copy link
Copy Markdown
Contributor

E2E Fixture Validation — Schema is up to date
16 value mismatches detected (expected — fixture represents an existing user).
View details

@sonarqubecloud
Copy link
Copy Markdown

@tommasini tommasini closed this Mar 20, 2026
@github-project-automation github-project-automation Bot moved this from Needs dev review to Merged, Closed or Archived in PR review queue Mar 20, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size-M team-mobile-platform Mobile Platform team

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[Sentry] Error: Migration 36: Invalid selectedAddress. state.engine.backgroundState.PreferencesController?.select...

2 participants